#include "mywidget.h"
#include "ui_mywidget.h"
#include <QtDebug>
#include <QColorDialog>

#include <QFileDialog>

#include <QFontDialog>

#include <QInputDialog>

#include <QMessageBox>

#include <QProgressDialog>

#include <QErrorMessage>

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{
    ui->setupUi(this);
}

MyWidget::~MyWidget()
{
    delete ui;
}

//显示颜色对话框
void MyWidget::on_pushButton_clicked()
{
    //第一种方法
//    QColor color = QColorDialog::getColor(
//                                            Qt::red,
//                                            this,
//                                            tr("颜色对话框"),
//                                            QColorDialog::ShowAlphaChannel
//                                         );

    //第二种方法
    QColorDialog dialog(Qt::red,this);                  //创建对象
    dialog.setOption(QColorDialog::ShowAlphaChannel);   //显示ALPHA通道
    dialog.exec();                                      //以模态方式运行对话框
    QColor color = dialog.currentColor();               //获取当前颜色

    qDebug()<<"color: " << color;
}

//文件对话框
void MyWidget::on_pushButton_2_clicked()
{
//    QFileDialog::getOpenFileNames() 获取多个文件
//    QFileDialog::getExistingDirectory() 获取文件夹
    QString fileName = QFileDialog::getOpenFileName(
                                                        this,
                                                        tr("文件对话框"),
                                                        "C:",
                                                        tr("图片文件( * png * jpg);;文本文件( * txt)")
                                                    );
    qDebug()<< "fileName:"<<fileName;
}


//字体对话框
void MyWidget::on_pushButton_3_clicked()
{
    //标记是否点击OK按钮
    bool ok;
    QFont font = QFontDialog::getFont(&ok,this);

    //点击OK则改变字体，点击cancel，那么输出信息
    if(ok) ui->pushButton_3->setFont(font);
    else qDebug() << tr("没有选择字体!");
}

//输入对话框
void MyWidget::on_pushButton_4_clicked()
{
   qDebug() << tr("输入对话框!");

   bool ok;
   QString string = QInputDialog::getText(this,
                                          tr("输入字符串对话框"),
                                          tr("请输入用户名:"),
                                          QLineEdit::Normal,
                                          tr("admin"),
                                          &ok
                                          );
   if (ok) qDebug() << "string:" << string;

   int value1 = QInputDialog::getInt(this,
                                     tr("输入整数对话框"),
                                     tr("请输入 -1000 到 1000 之间的数值"),
                                     100,
                                     -1000,
                                     1000,
                                     2,
                                     &ok
                                     );
   if (ok) qDebug() << "value1:" << value1;

   double value2 = QInputDialog::getDouble(this,
                                     tr("输入浮点数对话框"),
                                     tr("请输入 -1000 到 1000 之间的数值"),
                                     0.00,
                                     -1000,
                                     1000,
                                     2,
                                     &ok
                                     );
   if (ok) qDebug() << "value2:" << value2;


   QStringList items;
   items << tr("条目1") << tr("条目2");

   QString item = QInputDialog::getItem(this,
                                        tr("输入条目对话框"),
                                        tr("请选择一个条目"),
                                        items,
                                        0,
                                        true,
                                        &ok
                                     );
   if (ok) qDebug() << "item:" << item;
}


//消息对话框
void MyWidget::on_pushButton_5_clicked()
{
   qDebug() << tr("消息对话框!");

   int ret1 = QMessageBox::question(this,
                                    tr("问题对话框"),
                                    tr("你了解Qt吗？"),
                                    QMessageBox::Yes,
                                    QMessageBox::No
                                    );
   if (ret1 == QMessageBox::Yes) qDebug() << tr("问题！");

   int ret2 = QMessageBox::information(this,
                                    tr("提示对话框"),
                                    tr("这是Qt书籍"),
                                    QMessageBox::Ok
                                    );
   if (ret2 == QMessageBox::Ok) qDebug() << tr("提示");

   int ret3 = QMessageBox::warning(this,
                                    tr("警告对话框"),
                                    tr("不能提前结束"),
                                    QMessageBox::Abort
                                    );
   if (ret3 == QMessageBox::Abort) qDebug() << tr("警告！");

   int ret4 = QMessageBox::critical(this,
                                    tr("严重错误对话框"),
                                    tr("发现一个严重错误，要关闭"),
                                    QMessageBox::YesAll
                                    );
   if (ret4 == QMessageBox::YesAll) qDebug() << tr("错误");


   QMessageBox::about(this,tr("关于对话框"),tr("怼码大叔 怼码人生"));
}

//进度对话框
void MyWidget::on_pushButton_6_clicked()
{
   qDebug() << tr("进度对话框!");

   QProgressDialog dialog(tr("文件复制进度"),tr("取消"),0,5000,this);
   dialog.setWindowTitle(tr("进度对话框"));
   dialog.setWindowModality(Qt::WindowModal);
   dialog.show();
   for(int i = 0; i < 50000; i++){
       dialog.setValue(i);
       QCoreApplication::processEvents(); //避免界面冻结
       if (dialog.wasCanceled()) break;
   }
   dialog.setValue(50000);
   qDebug() << tr("复制结束");
}

//错误信息对话框
void MyWidget::on_pushButton_7_clicked()
{
   qDebug() << tr("错误信息对话框!");

   QErrorMessage *dialog = new QErrorMessage(this);
   dialog->setWindowTitle(tr("错误信息对话框"));
   dialog->showMessage(tr("这里是出错信息"));
   dialog->show();
}

//向导对话框
void MyWidget::on_pushButton_8_clicked()
{
   qDebug() << tr("向导对话框!");

   QWizard wizard(this);
   wizard.setWindowTitle(tr("向导对话框"));
   wizard.addPage(createPage1());
   wizard.addPage(createPage2());
   wizard.addPage(createPage3());
   wizard.exec();
}

QWizardPage * MyWidget::createPage1(){
   QWizardPage *page = new QWizardPage;
   page->setTitle(tr("介绍"));
   return page;
}

QWizardPage * MyWidget::createPage2(){
   QWizardPage *page = new QWizardPage;
   page->setTitle(tr("用户选择信息"));
   return page;
}

QWizardPage * MyWidget::createPage3(){
   QWizardPage *page = new QWizardPage;
   page->setTitle(tr("结束"));
   return page;
}
